<template>
    <el-dialog title="选择优惠券" :visible.sync="open" :before-close="close" append-to-body>
        <el-row>
            <el-button type="primary" @click="save">选择</el-button>
        </el-row>
        <el-row>
            <el-table :data="td" style="width:100%" height="350" @selection-change="hsc" tooltip-effect="dark" v-loading="loading" element-loading-text="数据加载中...">
                <el-table-column type="selection" width="55">
                </el-table-column>
                <el-table-column prop="couponName" label="优惠券名称">
                </el-table-column>
            </el-table>
        </el-row>
        <el-row>
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" style="float:right"
        :current-page="currentPage" :page-size="10" layout="total, prev, pager, next, jumper" :total="count">

            </el-pagination>
        </el-row>
    </el-dialog>
</template>
<script>
export default {
    data(){
        return{
            open:false,
            td:[],
            loading:false,
            selection:[],
            currentPage:1,
            count:0
        }
    },
    computed:{
        getstate(){
            return this.$store.state.coulistsclass
        }
    },
    watch:{
        getstate(v){
            this.td=[]
            this.getData()
            this.open=v
        }
    },
    methods:{
        hsc(v){//获取选中的值
            this.selection=v
        },
        close(){//清空列表
            this.td=[]
            this.$store.state.coulistsclass=false
        },
        handleSizeChange(i){//翻页
            var pp=new URLSearchParams()
            pp.append('pageNo',i)
            this.getData(pp)
        },
        handleCurrentChange(i){//翻页
            var pp=new URLSearchParams()
            pp.append('pageNo',i)
            this.getData(pp)
        },
        getData(v){//获取优惠券列表
            var _this=this
            this.loading=true
            this.td=[]
            axios.get('/listCoupon.do',{params:v}).then(r=>{
                if(r.data.code==200){
                    _this.td=[].concat(r.data.response.coupons)
                    _this.count=r.data.response.page.totalCount
                    _this.loading=false
                }else{
                    _this.$message.error(r.data.message)
                    _this.loading=false
                }
            })
        },
        save(){//将选中的值提交到父组件
            if(this.selection.length>1){
                this.$message({
                    message:'只能选择一项',
                    type:'info'
                })
            }
            // console.log("this.selection",this.selection)
            var ee=this.selection[0].couponId
            this.$emit("getcousid",ee)
            this.goodslist=false
            this.close()
        }
    }
}
</script>
<style>
.el-dialog__header {
    padding: 10px;
    background-color: #20a0ff!important;
}
</style>
